﻿using HCQ2_Common;
using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.DocModel.APIParam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_BLL
{
    public partial class T_VRWordSenseBLL: IT_VRWordSenseBLL
    {
        /// <summary>
        ///  获取单词释义列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<WordSenseModel> GetSenseTableData(WordSenseParam param,out int tatal)
        {
            List<WordSenseModel> result = new List<WordSenseModel>();
            List<T_VRWordSense> list = new List<T_VRWordSense>();
            if (!string.IsNullOrEmpty(param.keyword))
            {
                list = Select(s => s.word.Contains(param.keyword), s => s.ws_id, param.page, param.rows).ToList();
                tatal = SelectCount(s => s.word.Contains(param.keyword));
            }else
            {
                list = Select(s => true, s => s.ws_id, param.page, param.rows).ToList();
                tatal = SelectCount(s => true);
            }
            list?.ForEach(s => { result.Add(s.ToPOCO()); });
            return result;
        }
        /// <summary>
        ///  导入单词
        /// </summary>
        /// <param name="tempPath">execl路径</param>
        /// <returns></returns>
        public List<string> importWord(string tempPath)
        {
            //获取导入文件里面的数据
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List<string> valid = new List<string>();
            DataTable excelData = NpoiHelper.ExcelToDataTable(tempPath, true, 0);
            if (excelData == null)
                valid.Add("无法获取到excel文件中数据");
            sw.Stop();
            valid = checkRequired(excelData);
            if (valid.Count > 0)
                return valid;
            List<T_VRWordSense> list = toImportModelTable(excelData);
            sw.Restart();
            list.ForEach(s => { DBSession.IT_VRWordSenseDAL.Add(s); });
            sw.Stop();
            return valid;
        }
        /// <summary>
        /// 验证必填信息是否为空
        /// </summary>
        /// <param name="excelData"></param>
        /// <returns></returns>
        private List<string> checkRequired(DataTable excelData)
        {
            List<string> valid = new List<string>();
            for (int i = 0; i < excelData.Rows.Count; i++)
            {
                int rowNum = (i + 1 + 1);
                string word = Helper.ToString(excelData.Rows[i]["F1"]),
                    gj_symbol = Helper.ToString(excelData.Rows[i]["F2"]),
                    example = Helper.ToString(excelData.Rows[i]["F7"]);
                if (string.IsNullOrEmpty(word) || string.IsNullOrEmpty(word.Trim()))
                    valid.Add(string.Format("第{0}行，单词不能为空", rowNum));
                //if (string.IsNullOrEmpty(gj_symbol) || string.IsNullOrEmpty(gj_symbol.Trim()))
                //    valid.Add(string.Format("第{0}行，国际音标不能为空", rowNum));
                //判断解释长度是否超800
                if (!string.IsNullOrEmpty(example) && example.Length > 800)
                    valid.Add($"第{rowNum}行，例句字段长度超出了定义，要求长度控制在800以内，实际长度为{example.Length}");
                if (checkWord(word, Helper.ToString(excelData.Rows[i]["F4"])))
                    valid.Add(string.Format("第{0}行，单词【{1}】已经存在于单词名称和词性组合已在库存在！", rowNum, word));
            }
            return valid;
        }
        //验证是否存在
        private bool checkWord(string word,string cp_type)
        {
            //验证单词是否存在
            int mark = DBSession.IT_VRWordSenseDAL.SelectCount(s => s.word.Equals(word) && s.cp_type.Equals(cp_type));
            return mark > 0 ? true : false;
        }
        /// <summary>
        ///  数据转换为导入的数据
        /// </summary>
        /// <param name="excelData">原始数据</param>
        /// <returns></returns>
        private List<T_VRWordSense> toImportModelTable(DataTable excelData)
        {
            List<T_VRWordSense> list = new List<T_VRWordSense>();
            T_User user = HCQ2UI_Helper.OperateContext.Current.Usr;
            for (int i = 0; i < excelData.Rows.Count; i++)
            {
                list.Add(new T_VRWordSense
                {
                    word = Helper.ToString(excelData.Rows[i]["F1"]),
                    gj_symbol = Helper.ToString(excelData.Rows[i]["F2"]),
                    kk_symbol = Helper.ToString(excelData.Rows[i]["F3"]),
                    cp_type = Helper.ToString(excelData.Rows[i]["F4"]),
                    china_mean = Helper.ToString(excelData.Rows[i]["F5"]),
                    ws_phrase = Helper.ToString(excelData.Rows[i]["F6"]),
                    example = Helper.ToString(excelData.Rows[i]["F7"]),
                    create_name = user.user_name,
                    create_date = DateTime.Now,
                    update_name = user.user_name,
                    update_date = DateTime.Now
                });
            }
            return list;
        }
    }
}
